package fr.km.client.view.impl;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.km.client.event.SaveEvent;
import fr.km.client.event.SaveHandler;
import fr.km.client.model.Article;
import fr.km.client.model.ModelManager;
import fr.km.client.presenter.api.IArticleEditorPresenter;
import fr.km.client.presenter.impl.ArticleEditorPresenter;

public class ArticleFoundView extends Composite implements OpenHandler<DisclosurePanel> {
	private boolean isRead = false;
	private VerticalPanel disclosureTitle; 
	private Label title;
	private HTML content;
	private FlowPanel footTools;
	
	public ArticleFoundView(final Article article) {
		
		disclosureTitle = new VerticalPanel();
		
		title = new Label(article.getTitle());
		title.setStyleName("article-disclosure-title");
		
		String tag = new String();
		ArrayList<String> tags = article.getTags();
		Iterator<String> iterator = tags.iterator();
		while(iterator.hasNext()) {
			tag += iterator.next();
			if(iterator.hasNext()) {
				tag += ", ";
			}
		}
		
		Label tagsLabel = new Label(tag);
		tagsLabel.setStyleName("article-disclosure-tags");
		
		disclosureTitle.add(title);
		disclosureTitle.add(tagsLabel);
		
		content = new HTML(article.getContent());
		content.setStyleName("article-disclosure-content");
		
		footTools = new FlowPanel();
		footTools.setStyleName("foot-tools");
		
		Hyperlink edit = new Hyperlink("Edit", "edit");
		edit.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
//				Widget source = (Widget) event.getSource();
//	            int left = source.getAbsoluteLeft() + 10;
//	            int top = source.getAbsoluteTop() + 10;
//	            DialogBox popup = createArticleEditorPopup(article);
//	            popup.setModal(true);
//	            popup.center();
//	            popup.show();
				
				ArticleEditorView editor = new ArticleEditorView();
				IArticleEditorPresenter presenter = new ArticleEditorPresenter(article, new ModelManager());
				editor.setPresenter(presenter);
				presenter.setView(editor);
				presenter.loadArticle();
				presenter.displayArticle();
			}
			
		});
		
		footTools.add(edit);
		
		VerticalPanel contentPanel = new VerticalPanel();
		contentPanel.setWidth("100%");
		
		contentPanel.add(content);
		contentPanel.add(footTools);
		
		DisclosurePanel p = new DisclosurePanel(disclosureTitle);
		p.setContent(contentPanel);
		p.addOpenHandler(this);
		
		initWidget(p);
		
		p.setStyleName("article-disclosure");
	}

	@Override
	public void onOpen(OpenEvent<DisclosurePanel> event) {
		if(!isRead) {
			isRead = true;
			disclosureTitle.setStyleName("article-disclosure-read");
			title.setStyleName("article-disclosure-title-read");
		}
		
	}
	
//	private DialogBox createArticleEditorPopup(final Article article) {
////		final DialogBox popup = new DialogBox();
////		popup.setTitle("Edit");
//		
//		ArticleEditorView editor = new ArticleEditorView();
//		IArticleEditorPresenter presenter = new ArticleEditorPresenter(article);
//		editor.setPresenter(presenter);
//		presenter.setView(editor);
//		presenter.loadArticle();
//		presenter.displayArticle();
//		
////		editor.addSaveHandler(new SaveHandler() {
////
////			@Override
////			public void onSave(SaveEvent event) {
////				popup.hide();				
////			}
////			
////		});
////		
////		editor.addCloseHandler(new CloseHandler<ArticleEditorView>() {
////
////			@Override
////			public void onClose(CloseEvent<ArticleEditorView> event) {
////				popup.hide();	
////			}
////			
////		});
////		
////		popup.setWidget(editor);
//		return popup;
//		
//	}
}
